package com.gpt.article.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.gpt.article.domain.GptGenArticleTask;
import com.gpt.article.domain.GptTopicTitle;
import com.gpt.article.mapper.GptTxtClassDetailMapper;
import com.gpt.article.service.IGptTxtClassDetailService;
import com.gpt.article.service.impl.taskHandle.GenTopicArticleTask;
import com.gpt.article.service.impl.taskHandle.GenTxtClassifyTask;
import com.gpt.article.service.impl.taskHandle.TaskSplite;
import com.gpt.article.service.impl.taskHandle.task.GenTxtClassifyTasker;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.GptTxtClassDetail;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gpt.article.mapper.TxtClassifyMapper;
import com.gpt.article.domain.TxtClassify;
import com.gpt.article.service.ITxtClassifyService;

/**
 * 内容分类Service业务层处理
 * 
 * @author zhuzi
 * @date 2023-12-14
 */
@Service
public class TxtClassifyServiceImpl implements ITxtClassifyService 
{
    private static Logger logger = LoggerFactory.getLogger(TxtClassifyServiceImpl.class);

    @Autowired
    private TxtClassifyMapper txtClassifyMapper;
    @Autowired
    private GptGenArticleTaskServiceImpl gptGenArticleTaskServiceImpl;
    @Autowired
    private GptTxtClassDetailMapper gptTxtClassDetailMapper;

    @Autowired
    private IGptTxtClassDetailService gptTxtClassDetailService;
    private TaskSplite taskSplite;
    /**
     * 查询内容分类
     * 
     * @param id 内容分类主键
     * @return 内容分类
     */
    @Override
    public TxtClassify selectTxtClassifyById(Long id)
    {
        return txtClassifyMapper.selectTxtClassifyById(id);
    }

    /**
     * 查询内容分类列表
     * 
     * @param txtClassify 内容分类
     * @return 内容分类
     */
    @Override
    public List<TxtClassify> selectTxtClassifyList(TxtClassify txtClassify)
    {
        return txtClassifyMapper.selectTxtClassifyList(txtClassify);
    }

    @Override
    public List<GptTxtClassDetail> selectTxtClassList(Long itemId)
    {
        return gptTxtClassDetailMapper.selectGptTxtClassDetailByitemId(itemId);
    }

    @Override
    public List<GptTxtClassDetail> selectTxtClassListByParentId(Long parentId)
    {
        return gptTxtClassDetailMapper.selectGptTxtClassDetailByParentId(parentId);
    }

    /**
     * 新增内容分类
     * 
     * @param txtClassify 内容分类
     * @return 结果
     */
    @Override
    public Long insertTxtClassify(TxtClassify txtClassify)
    {
        txtClassify.setCreateTime(DateUtils.getNowDate());
        Long id = txtClassifyMapper.insertTxtClassify(txtClassify);
        return id;
    }

    /**
     * 修改内容分类
     * 
     * @param txtClassify 内容分类
     * @return 结果
     */
    @Override
    public Long updateTxtClassify(TxtClassify txtClassify)
    {
         txtClassify.setUpdateTime(DateUtils.getNowDate());
        Long id = txtClassifyMapper.updateTxtClassify(txtClassify);
        return id;
    }

    /**
     * 批量删除内容分类
     * 
     * @param ids 需要删除的内容分类主键
     * @return 结果
     */
    @Override
    public int deleteTxtClassifyByIds(Long[] ids)
    {
        return txtClassifyMapper.deleteTxtClassifyByIds(ids);
    }

    /**
     * 删除内容分类信息
     * 
     * @param id 内容分类主键
     * @return 结果
     */
    @Override
    public int deleteTxtClassifyById(Long id)
    {
        return txtClassifyMapper.deleteTxtClassifyById(id);
    }






    /**
     * 生成内容分类
     * @param ids
     * @return
     */
    @Override
    public int genGptClassify(Long[] ids, String acc, Map<String,Object> map){
        List<TxtClassify> txtClassifies = txtClassifyMapper.selectTxtClassifyListByIds(ids);
        System.out.println(map+">>>>>>");
        Boolean useClassItem = Boolean.valueOf(String.valueOf(map.get("useClassswitch")));
        if(useClassItem){
            Long itemId = Long.valueOf(String.valueOf(map.get("itemId")));
            GptTxtClassDetail gptTxtClassDetail = new GptTxtClassDetail();
            gptTxtClassDetail.setItemId(itemId);
            List<TreeSelect> treeSelects = gptTxtClassDetailService.selectTxtClassTreeList(gptTxtClassDetail);
            GptTxtClassDetail topClass = selectTxtClassListByParentId(0l).get(0);
//            System.out.println(topClass.getName());
            map.put("topClass",topClass.getName());
            map.put("classinfo",treeSelects);
//            System.out.println("分类信息："+treeSelects);
        }
        Integer xigaoAllNum = genStart(txtClassifies,acc,map);
        System.out.println("本批次任务数：："+txtClassifies.size());
        return txtClassifies.size();
    }

    @Override
    public int genManyGptClassify(TxtClassify txtClassify, String acc, Map<String, Object> map) {
        List<TxtClassify> txtClassifies = new ArrayList<TxtClassify>();
        boolean findAll = false;
        Integer taskSum = txtClassifyMapper.getTxtContentCount(txtClassify);
        if(txtClassify.getGennum()==0){
            findAll = true;
        }
        if(!findAll){
            taskSum = taskSum>txtClassify.getGennum()?txtClassify.getGennum():taskSum;
        }
        logger.info("当前任务条数:::{}",taskSum);
        //小于200则不分片 直接批量执行即可
        if(taskSum<200&&taskSum>0){
            txtClassifies = txtClassifyMapper.selectTxtClassifyList(txtClassify);
            genStart(txtClassifies,acc,map);
        }else {
            //本次任务id
            List<Long> ids = txtClassifyMapper.selectIds(txtClassify);
            //任务分片
            taskSplite = new TaskSplite.Builder()
                    .setSource(ids)
                    .setAcc(acc)
                    .setSpan(128)//分片间隔，相对应应用来说单次执行量
                    .setTaskType("txtClassify")
                    .setPsMap(map)
                    .build();
            taskSplite.process();
        }
        return taskSum;
    }


    /**
     * 创建任务
     * @param txtClassifies
     * @return
     */
    public Integer genStart(List<TxtClassify> txtClassifies,String acc , Map<String,Object> map){
        GptGenArticleTask genArticleTask = new  GptGenArticleTask();
        genArticleTask.setProce(0);
        genArticleTask.setArticleNum(txtClassifies.size());
        genArticleTask.setStatus("1");
        genArticleTask.setSuccessNum(0);
        genArticleTask.setErrorNum(0);
        genArticleTask.setCreateTime(DateUtils.getNowDate());
        Long taskId =gptGenArticleTaskServiceImpl.insertGptGenArticleTask(genArticleTask);
//         gptGenArticleTaskMapper.insertGptGenArticleTask(genArticleTask);
//        Long taskId = genArticleTask.getFileId();
        //开启任务 GenTxtClassifyTask
        Integer xigaoAllNum = new GenTxtClassifyTask().createContentClassifyTask(taskId,acc,txtClassifies,map);
        return xigaoAllNum;
    }
}
